/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.gui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import de.fuberlin.kosmik.R;
import de.fuberlin.kosmik.StrUtils;
import de.fuberlin.kosmik.data.osmt.OsmtData;
import de.fuberlin.kosmik.data.osmt.OsmtDataFactory;
import de.fuberlin.kosmik.data.osmt.OsmtFeature;
import de.fuberlin.kosmik.data.osmt.OsmtObjectException;
import de.fuberlin.kosmik.gui.view.adapter.EditKeyAdapter;
import de.fuberlin.kosmik.gui.view.adapter.EditValueAdapter;
import de.fuberlin.kosmik.log.Constants;

/**
 * TagEditingActivity shows the user two AutoCompleteEditViews to modify a key
 * value pair.
 * 
 * @author Johannes
 * @see TagBundleActivity
 * @see TagPickerActivity
 * 
 */
public class TagEditingActivity extends Activity {

	private static final String TAG = Constants.TAG_EDITING_ACTIVITY;
	/**
	 * View for editing the key.
	 */
	protected AutoCompleteTextView edtKeyView;
	/**
	 * View for editing the value.
	 */
	protected AutoCompleteTextView edtValueView;

	private OsmtData osmtData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// setTitle( R.string.view__title);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tag_editing);

		// Get access to database
		osmtData = OsmtDataFactory.createOsmtData(this);

		// setup the user interface
		initViews();

		// process params
		processParams();
	}

	/**
	 * @see EditKeyAdapter
	 * @see EditValueAdapter
	 */
	private void initViews() {

		// key editor
		edtKeyView = (AutoCompleteTextView) findViewById(R.id.tagEditing_keyAutoCompleteTextView);
		edtKeyView.setAdapter(new EditKeyAdapter(this));

		edtKeyView.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {
					refreshComment(edtKeyView.getText().toString(), edtValueView.getText()
							.toString());
				}
			}
		});

		// value editor
		edtValueView = (AutoCompleteTextView) findViewById(R.id.tagEditing_valueAutoCompleteTextView);
		edtValueView.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (hasFocus) {
					edtValueView.setAdapter(new EditValueAdapter(TagEditingActivity.this,
							edtKeyView.getText().toString()));
				} else {
					refreshComment(edtKeyView.getText().toString(), edtValueView.getText()
							.toString());
				}
			}
		});

	}

	/**
	 * Analyze the parameter intent and prepopulate the key and value fields.
	 */
	private void processParams() {
		final Bundle extras = getIntent().getExtras();

		final String keyName = extras.getString("KEY");
		if (keyName != null)
			edtKeyView.setText(keyName);

		final String valueName = extras.getString("VALUE");
		if (valueName != null)
			edtValueView.setText(valueName);

		refreshComment(keyName, valueName);

	}

	/**
	 * Called when the Cancel-button is pressed.
	 * 
	 * @param view the View in which the cancel click action has occurred
	 */
	public void onCancelClick(View view) {
		Log.d(TAG, "cancel clicked.");
		setResult(RESULT_CANCELED, new Intent());
		finish();
	}

	/**
	 * Called when the Ok-button is pressed.
	 * 
	 * @param view the View in which the ok click action has occurred
	 */
	public void onOkClick(View view) {
		String key = edtKeyView.getText().toString();
		String value = edtValueView.getText().toString();
		Log.d(TAG, String.format("ok clicked with key: %s, value: %s", key, value));
		Intent resultIntent = new Intent();
		resultIntent.putExtra("KEY", key);
		resultIntent.putExtra("VALUE", value);
		// set result values
		setResult(RESULT_OK, resultIntent);
		// and return to super activity
		finish();
	}

	/**
	 * Load the comment for the given key and value form the database and update
	 * the edit view for the comment.
	 * 
	 * @param keyName The key
	 * @param valueName The value
	 */
	protected void refreshComment(String keyName, String valueName) {
		final TextView commentView = (TextView) findViewById(R.id.tagEditing_commentTextView);
		try {
			OsmtFeature feature = new OsmtFeature(osmtData, keyName, valueName, null, null,
					null, null, null, null, null);
			final String foundedComment = feature.get().comment;
			if (StrUtils.isNotEmpty(foundedComment)) {
				commentView.setText(foundedComment);
			}
		} catch (OsmtObjectException e) {
			commentView.setText("");
		}
	}
}
